package servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;

import database.DatabaseManager;
import database.User;

/**
 * Servlet implementation class RegisterServlet
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * Register
     */
    private void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	request.setCharacterEncoding("utf-8");
    	String name = request.getParameter("name");
    	String password = request.getParameter("password");
    	String password1 = request.getParameter("password1");
    	String role = "u";
    	String email = request.getParameter("email");
    	if(name == null || password == null || password1 == null || email == null) {
    		response.sendRedirect(request.getContextPath() + "/registerFailed.jsp?name=" + name + "&password=" + password + "&password1=" + password1 + "&email=" + email);
    		return;
    	}
    	if(name.equals("") || password.equals("") || password1.equals("") || email.equals("")) {
    		response.sendRedirect(request.getContextPath() + "/registerFailed.jsp?name=" + name + "&password=" + password + "&password1=" + password1 + "&email=" + email);
    		return;
    	}
    	if(!password.equals(password1)) {
    		response.sendRedirect(request.getContextPath() + "/registerFailed.jsp?name=" + name + "&password=" + password + "&password1=" + password1 + "&email=" + email);
    		return;
    	}
    	//TODO sql
    	boolean b = DatabaseManager.userRegister(name, password, role, new Date(), email);
    	if(!b) {
    		//TODO
    		response.sendRedirect(request.getContextPath() + "/registerFailed.jsp?name=" + name + "&password=" + password + "&password1=" + password1 + "&email=" + email);
    		return;
    	}
    	
    	HttpSession session = request.getSession();
    	session.setAttribute("name", name);
    	/**
    	 * TODO permission
    	 * session.setAttribute("permission", permission);
    	 */
    	session.setAttribute("permission", role);
    	response.sendRedirect(request.getContextPath() + "/index.jsp");
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.register(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.register(request, response);
	}

}
